package com.rpj.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.rpj.entity.StuDto;
import com.rpj.entity.StuEntity;
import com.rpj.service.StuService;


@RestController
public class StuController {
@Autowired
StuService stuservice;

@RequestMapping(value="/query")
public Object queryall(StuDto dto,int page,int limit) {
	Pageable pageable = PageRequest.of(page-1, limit);
	Page<StuEntity> pageStu = stuservice.queryByCondition(dto, pageable);
	HashMap<String,Object> map = new HashMap<String,Object>();
	map.put("code",0);
	map.put("msg", "");
	map.put("count", pageStu.getTotalElements());
	map.put("data", pageStu.getContent());
	
	return map;
}

@RequestMapping(value="/update",method = RequestMethod.POST)
public Object update(String stuname,String stupassword,String sex,String status,
		String department,String specialty,String degree,String stuborrowbooknum,String stuborrowmaxnum,String stugivebooknum,String data1,String data2,String data3,String id)
 {
	int updateStu = stuservice.updateStu(stuname, stupassword, sex, status, department, specialty, degree, stuborrowbooknum, stuborrowmaxnum, stugivebooknum,data1,data2,data3, id);
	System.out.println(updateStu);
	if(updateStu>0) {
		return true;	
	}else {
		return false;
	}
}
@RequestMapping(value="/add",method = RequestMethod.POST)
public Object save(StuEntity stu) {
	StuEntity save = stuservice.save(stu);
	System.out.println(save);
	if(save!=null) {
		return true;
	}else {
		return false;
	}
	
}
@RequestMapping(value="/queryone")
public Object findone(StuEntity stuid) {
	StuEntity findone = stuservice.findone(stuid);
	return findone;
}
/**
 * 查询所有专业
 */
@RequestMapping(value="/queryMajor",method = RequestMethod.POST)
public Object queryMajor() {
	return stuservice.selMajor();
}
//查询名字，借书，还书
@RequestMapping(value="/queryBar",method =RequestMethod.POST)
public Object queryBar() {
	List<StuEntity> list = stuservice.queryall();
	List<String> stuname=new ArrayList<>();
	List<String> stuborrowbooknum=new ArrayList<>();
	List<String> stugivebooknum=new ArrayList<>();

	for (StuEntity stu : list) {
		stuname.add(stu.getStuname());
		stuborrowbooknum.add(stu.getStuborrowbooknum());
		stugivebooknum.add(stu.getStugivebooknum());
	}
	Map<String,Object> map = new HashMap<String, Object>();
	map.put("name",stuname );
	map.put("borrow", stuborrowbooknum);
	map.put("given", stugivebooknum);
	return map;
}
//登陆查询
@RequestMapping(value="/stulogin")
public Object stulogin(String stuname,String stupassword,HttpServletResponse response) {
	StuEntity i = stuservice.stulogin(stuname, stupassword);
	System.out.println(i);
	HashMap<String, Object> map = new HashMap<String, Object>();
	ArrayList<StuEntity> list = new ArrayList<StuEntity>();
	list.add(i);
	map.put("code",0);
	map.put("data",list);
	map.put("count",1);
	map.put("msg","");
	if(i!=null) {
		
			//response.sendRedirect("/statistics/admin/login.html");

		return map;
	}else {
		return map;
	}
}
}
